﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;
namespace QldtSdh.GUI.DaoTao
{
    public partial class FormInThongKeGioGiang : BaseForm
    {
        private string filePathTemp = Application.StartupPath + "\\{0}.doc";
        private int timeToWaitExport = 3000;
        WordUtil word = null;
        public FormInThongKeGioGiang()
        {
            InitializeComponent();
            var rs = ConfigUtil.GetConfig("timetowaitexport");
            if (rs != null && rs.Count == 1)
                try
                {
                    timeToWaitExport = int.Parse(rs[0].Value);
                }
                catch
                {
                    timeToWaitExport = 3000;
                }
        }

        private void FormInBangDiem_Load(object sender, EventArgs e)
        {

        }

        public void ShowOrPrint(HocPhan hocPhan, LopHocPhan lopHocPhan, List<HocVien> dsDuThi, DotThi_LopHocPhan thongTinMonThi, bool preview = true, bool printMultifile = false)
        {
            HocVienBindingSource.DataSource = dsDuThi;
            ReportParameter[] parameters = new ReportParameter[4];
            parameters[0] = new ReportParameter("rpTenLopHocPhan", lopHocPhan.TenLopHocPhanFull);
            parameters[2] = new ReportParameter("rpPhongThi", thongTinMonThi.MaPhongHoc);
            parameters[3] = new ReportParameter("rpNgayThi", string.Format("{0:dd/MM/yyyy}", thongTinMonThi.NgayThi.HasValue ? thongTinMonThi.NgayThi.Value : DateTime.MinValue));
            parameters[1] = new ReportParameter("rpSoTinChi", hocPhan.SoTinChi.ToString());
            System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
            pg.Margins.Top = 30;
            pg.Margins.Bottom = 20;
            pg.Margins.Left = 20;
            pg.Margins.Right = 20;
            pg.Landscape = false;
            PaperSize size = new PaperSize();
            size.Width = 827;
            size.Height = 1169;
            pg.PaperSize = size;
            this.rvBangDiem.SetPageSettings(pg);
            this.rvBangDiem.LocalReport.SetParameters(parameters);

            this.rvBangDiem.RefreshReport();
            rvBangDiem.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            rvBangDiem.PrinterSettings.PrinterName = PrintUtil.GetDefaultPrinter();
            if (!preview)
            {
                string fileName = string.Format(filePathTemp, lopHocPhan.MaLopHocPhanGUID.Replace(".", ""));
                LocalReport report = this.rvBangDiem.LocalReport;
                PrintUtil.Export(report, PrintUtil.WORDFORMAT, fileName);
                Thread.Sleep(timeToWaitExport);
                if (word == null)
                    word = new WordUtil();
                word.PrintDocument(fileName);
            }
            else this.ShowDialog();
        }

    }
}
